package com.example.androidcompletesec.xmlparser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.androidcompletesec.R;

public class Dom_parserActivity extends Activity {
	ListView list;
	ProgressDialog progressDlg;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network);
		list = (ListView) findViewById(R.id.listView);
		new ParsingXml().execute("order.xml");
	}
	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>> {
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(Dom_parserActivity.this, "Wait",
					"order.xml & order.json Making ...");
		}
		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(Dom_parserActivity.this,
					R.layout.itemrow, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}
		protected ArrayList<Item> doInBackground(String... params) {
			StringBuffer sb = new StringBuffer();
			String str = null;
			try {
				BufferedReader fis = new BufferedReader(new InputStreamReader(
						openFileInput(params[0])));
				while ((str = fis.readLine()) != null) {
					sb.append(str);
				}
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}
	}
	public ArrayList<Item> parsingXml(String xml) {
		ArrayList<Item> arItem = new ArrayList<Item>();
		Item itemClass;
		try {
			InputStream istream = new ByteArrayInputStream(xml.getBytes("utf-8"));

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(istream);  //루트 읽어드림..(or3 import)

			Element order = doc.getDocumentElement();
			NodeList items = order.getElementsByTagName("item");
			for (int i = 0; i < items.getLength(); i++) {
				itemClass = new Item();
				Node itemNode = items.item(i);
				Node itemValue = itemNode.getFirstChild();
				itemClass.setItemName(itemValue.getNodeValue());

				if(itemNode.hasAttributes()){
					NamedNodeMap Attrs = itemNode.getAttributes();
					for (int j = 0; j < Attrs.getLength(); j++) {
						Node attr = Attrs.item(j);
						if (attr.getNodeName().equalsIgnoreCase("price")) {
							itemClass.setItemPrice(Integer.parseInt(attr.getNodeValue()));
						}else{
							itemClass.setMakerName(attr.getNodeValue());
						}
					}
				}
				arItem.add(itemClass);
			}
		} catch (Exception e) { Log.i("Dom_Parser", e.getMessage()); }
		return arItem;
	}
}
